import java.lang._
import java.io._
import scala.collection.mutable._
import scala.actors.Actor._

object Shell {
    
  
  def createMap(parser:actors.Actor,lines:HashMap[String,String])={
    actor {
      var continue = true
      loopWhile(continue){
	  receive{
	    case "quit" => continue =false
	    lines.foreach(x=>println(x._1 + ";" + x._2))
	    parser ! "ok"
	    case symb:String      =>
		if(symb.contains("  ")){
		  
		}
		else
		  {
		    val tokens = symb.split(" ",3)
		    //printf ("@:%s type:%s sym:%s\n",tokens(0),tokens(1),tokens(2))
		    lines+=tokens(0)->tokens(2)
		    //lines.put("a" ,"b")				
		  }
	  }
	
	}
      
    }
  }
  
  
  def readTextSegment(console:actors.Actor,file:String){
    var line:String = ""
    
    /*execution de la commande nm*/
    val proc =Runtime.getRuntime().exec(Array("nm","--demangle",file))
    
    proc.wait()
    
    if(proc.exitValue()!=0){
      println("Fichier inexistant")
      console ! "fail"
    }
    else{
      /*recuperation de la sortie de la commande*/
      val reader:BufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream()))
      do {
	line = reader.readLine()
	if(line!=null){
	  console ! line
	}
      }while(line!=null)
      
      console ! "quit"
    }	 
  }
      
  def main(args: Array[String])  = {
    
    
    if(args.length!=1){
      println("Usage: scala Shell [exec file]")
    }
    else{
      var lines : HashMap [String,String]= new HashMap[String,String]
      val parser = self
	
      /*creation de l'acteur mappeur*/
      val console= createMap(parser,lines)
      
      /*traitment de la sortie de la commande nm*/
      readTextSegment(console,args(0))   
      
      /*reception de la fin*/
      receive{
	case "ok"   => println("fin")
	case "fail" => println("end")
	case  _     =>
      }
    }
    null
  }
}
